package com.example.foodisimportant.StudentViewActivity.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.foodisimportant.Adapter.ViewAdapter;
import com.example.foodisimportant.Bean.PostEntity;
import com.example.foodisimportant.Body.ResponseBody;
import com.example.foodisimportant.Canteens.DishInformationActivity;
import com.example.foodisimportant.Network.NetworkSettings;
import com.example.foodisimportant.Network.SearchNetworkThread;
import com.example.foodisimportant.R;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.concurrent.FutureTask;

public class ViewFragment extends Fragment {
    View viewLayout;
    private ArrayList<PostEntity> postEntityArrayList=new ArrayList<PostEntity>();
    private RecyclerView mViewRecyclerView;
    private ViewAdapter mViewRecyclerAdapter;
    private final ObjectMapper mapper = new ObjectMapper();
    static String topdish="1";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        viewLayout=inflater.inflate(R.layout.fragment_view,container,false);
        initRecyclerView();
        new Thread(new Runnable() {
            @Override
            public void run() {
                FutureTask<String> topdishTask = new FutureTask<>(new SearchNetworkThread(topdish, NetworkSettings.SIGN_IN));
                Thread thread =new Thread(topdishTask);
                thread.start();
                try{
                    ResponseBody body = mapper.readValue(topdishTask.get(), ResponseBody.class);
                    JSONArray array=new JSONArray(body.getData());
                    for (int i=0;i<array.length();i++){
                        JSONObject object=array.getJSONObject(i);
                        String dishName=object.getString("dishName");
                        String dishPosition=object.getString("dishPosition");
                        String dishstrImg=object.getString("dishstrImg");
                        String dishPrice=object.getString("dishPrice");
                        PostEntity postEntity=new PostEntity();
                        postEntity.setName(dishName);
                        postEntity.setPosition(dishPosition);
                        postEntity.setPrice(dishPrice);
                        postEntity.setNumber("TOP"+i);
                        postEntity.setPicture(dishstrImg);
                        postEntityArrayList.add(postEntity);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
        return viewLayout;
    }


    /**
     * 对recycleview进行设置
     */
    private void initRecyclerView(){
        //获取RecyclerView
        mViewRecyclerView=viewLayout.findViewById(R.id.rv_review);
        //创建adapter
        mViewRecyclerAdapter = new ViewAdapter(getActivity(),postEntityArrayList);
        //给RecyclerView设置adapter
        mViewRecyclerView.setAdapter(mViewRecyclerAdapter);
        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
        //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
        mViewRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        //设置item的分割线
        mViewRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        //RecyclerView中没有item的监听事件，需要自己在适配器中写一个监听事件的接口。参数根据自定义
        mViewRecyclerAdapter.setOnItemClickListener(new ViewAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, PostEntity data) {
                Intent intent=new Intent(getActivity(), DishInformationActivity.class);
                startActivity(intent);
            }
        });
    }

}
